<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;

$field = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
?>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type='.$objectType));?>">
    <?php
    if ($field != null) {
    ?>
    <input
        type="hidden"
        name="id"
        value="<?php Printer::display($field->getId()); ?>" />
    <?php
    }
    ?>
    <div class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="name"
            value="<?php if (!is_null($field)) Printer::display($field->getName()); ?>" />
    </div>
    <div class="label">
        <span class="titre">Type de champs :</span>
        <?php
        $fieldTypeId = '' ;
        if (!is_null($field) && !is_null($field->getType())) {
            $fieldTypeId = $field->getType()->getId();
            ?>
        <span class="value">
            <?php Printer::display($field->getType()->toString()); ?>
        </span>
        <?php
        } else {
        ?>
        <input class="firstInput fieldTypeSelecter" type="text" />
        <?php 
        }
        ?>
        <input
            type="hidden"
            id="fieldTypeSelecterId"
            name="field_type"
            value="<?php Printer::display($fieldTypeId); ?>" />
    </div>
    <div class="label">
        <span class="titre">Valeurs des paramètres du champ :</span>
        <div id="fieldValues"></div>
    </div>
    <input type="submit" value="OK" />
</form>
<script type="text/javascript">
    function createValuesInputs(structure, values) {
        if (typeof(values) == 'undefined') values = null ;
        if (structure != null) {
            var fields = JSON.parse(structure);
            for(var i=0; i<fields.length; i++) {
                var superField = fields[i] ;
                for (var j=0; j<superField.elements.length; j++) {
                    var field = superField.elements[j] ;
                    if (field.label != null) {
                        if (
                            field.label.startsWith('<') &&
                            field.label.endsWith('>')
                        ) {
                            var label = field.label ;
                            var parsedLabel = label.substr(1, label.length-2);
                            var value = '' ;
                            if (
                                values != null &&
                                typeof(values[parsedLabel]) != 'undefined'
                            ) {
                                value = values[parsedLabel] ;
                            } 
                            var div =
                                '<div class="label">'+
                                    '<span class="titre">'+
                                       '&lt;' + parsedLabel + '&gt; :'+
                                    '</span>'+
                                    '<textarea '+
                                        'class="firstInput" '+
                                        'name="field_value_'+parsedLabel+'" >'+
                                        value+
                                    '</textarea>'+
                                '</div>' ;
                            $("#fieldValues").append(div);
                        }
                    }
                }
            }
        }
    }

    $(document).ready(function() {
        $(".fieldTypeSelecter").autocomplete({
            source: "?service=ListFieldTypes&format=json",
            select: function(event, ui) {
                $(".fieldTypeSelecter").val(ui.item.label);
                $("#fieldTypeSelecterId").val(ui.item.id);
                createValuesInputs(ui.item.structure);
            }
        });
    
        <?php 
            if (!is_null($field)) {
                $values = $field->getValues();
                
                $s = $field->getType()->getStructure()->serialize();
                
                if (!is_null($values)) {
                    $v = $values->serialize();
        ?>
        createValuesInputs('<?php echo $s ; ?>', <?php echo $v ; ?>);
        <?php 
                }
            }
        ?>
    });
</script>
